package com.iflytek.autofly.setting.biz.base;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.util.HashMap;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/12/9</td>
 * <td>10:43</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class BottomMenu implements View.OnClickListener {
    private static BottomMenu sCurrent = null;
    private final View mView;
    private final LinearLayout mMenuLl = null;
    private Activity mActivity;
    private PopupWindow mPopWindow;
    private HashMap mListeners = new HashMap<Integer, View.OnClickListener>();

    public BottomMenu(Activity activity, int resId) {
        mView = activity.getLayoutInflater().inflate(resId, null);
        mPopWindow = new PopupWindow(mView, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        mPopWindow.setFocusable(true);
        mPopWindow.setBackgroundDrawable(new Drawable() {
            @Override
            public void setColorFilter(ColorFilter arg0) {
            }

            @Override
            public void setAlpha(int arg0) {
            }

            @Override
            public int getOpacity() {
                return 0;
            }

            @Override
            public void draw(Canvas arg0) {
            }
        });
        mPopWindow.setOutsideTouchable(true);

        mPopWindow.update();

        mActivity = activity;

        sCurrent = this;
    }

    public static void clear() {
        if (sCurrent != null) {
            try {
                sCurrent.dismiss();
            } catch (Exception ignore) {
            }
            sCurrent = null;
        }
    }

    public BottomMenu setButtonListener(int resId, View.OnClickListener l) {
        View v = mView.findViewById(resId);
        if (v != null) {
            v.setOnClickListener(this);
            mListeners.put(resId, l);
        }
        return this;
    }

    public BottomMenu setOnDismissListener(PopupWindow.OnDismissListener l) {
        mPopWindow.setOnDismissListener(l);
        return this;
    }

    public BottomMenu show() {
        mPopWindow.showAtLocation(mActivity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        return this;
    }

    @Override
    public void onClick(View v) {
        mPopWindow.dismiss();
        View.OnClickListener l = (View.OnClickListener) mListeners.get(v.getId());
        if (l != null) {
            l.onClick(v);
        }

        if (sCurrent == this) {
            sCurrent = null;
        }
    }

    public void dismiss() {
        mPopWindow.dismiss();
        if (sCurrent == this) {
            sCurrent = null;
        }
    }

}
